
--local CdTimeBuilder = gf.utils.class("CdTimeBuilder")
CdTimeBuilder = CdTimeBuilder or {}

---1、大于1天时只显示到天, 不足1天时显示时分秒
---2、不显示天, 全部叠加在小时上
---3、不显示秒

local Day_Seconds = 60 * 60 * 24
local Hour_Seconds = 60 * 60
local Min_Seconds = 60

local _dayUnit = "天"
local _hourUnit = "时"
local _minUnit = "分"
local _secUnit = "秒"

local _numStr = { "01", "02", "03", "04", "05", "06", "07", "08", "09", [0] = "00"}


function CdTimeBuilder.setDefaultUnit(dayUnit, hourUnit, minUnit, secUnit)
    _dayUnit = dayUnit
    _hourUnit = hourUnit
    _minUnit = minUnit
    _secUnit = secUnit
end

function CdTimeBuilder.new()
    local inst = {}

    local concatTb = {"00", _dayUnit, "00", _hourUnit, "00", _minUnit, "00", _secUnit}
    local showZeroDay = false
    local showSec = true
    local noTimeHaveDay = false --有天数时不显示时间

    function inst:setUnit(dayUnit, hourUnit, minUnit, secUnit)
        if nil ~= dayUnit then concatTb[2] = dayUnit end
        if nil ~= hourUnit then concatTb[4] = hourUnit end
        if nil ~= minUnit then concatTb[6] = minUnit end
        if nil ~= secUnit then concatTb[8] = secUnit end
    end

    function inst:setShowZeroDay(b)
        showZeroDay = b
    end

    function inst:setShowSec(b)
        showSec = b
    end

    function inst:setNoTimeHaveDay(b)
        noTimeHaveDay = b
    end

    ---100天16时09分03秒,
    ---# >=1天时是否只显示天数用noTimeHaveDay控制,
    ---# 0天时是否显示天用showZeroDay控制,
    ---# 是否显示秒用showSec控制
    function inst:updateString(second)
        local day = math.floor(second / Day_Seconds)
        concatTb[1] = _numStr[day] or tostring(day)
        second = second - day * Day_Seconds

        if day > 0 and noTimeHaveDay then
            return table.concat(concatTb, nil, 1, 2)
        end

        local hour = math.floor(second / Hour_Seconds)
        concatTb[3] = _numStr[hour] or tostring(hour)
        second = second - hour * Hour_Seconds

        local min = math.floor(second / Min_Seconds)
        concatTb[5] = _numStr[min] or tostring(min)
        second = second - min * Min_Seconds

        local sec = math.floor(second)
        concatTb[7] = _numStr[sec] or tostring(sec)

        local concatEndIndex = (true == showSec) and 8 or 6
        if day <= 0 and false == showZeroDay then
            return table.concat(concatTb, nil, 3, concatEndIndex)
        end

        return table.concat(concatTb, nil, 1, concatEndIndex)
    end

    setmetatable(inst, inst)
    return inst
end




local function Test1()
    local obj = CdTimeBuilder.new()
    --obj:setNoTimeHaveDay(true)
    obj:setShowSec(false)
    print(obj:updateString(1211134))
end

Test1()